package com.situ.mall.admin.controller;


import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.situ.mall.base.entity.Category;
import com.situ.mall.base.service.ICategoryService;
import com.situ.mall.base.util.C;
import com.situ.mall.base.util.URLUtil;
import com.situ.mall.base.vo.ResVO;
import com.situ.mall.base.vo.LayuiTableVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author darling
 * @since 2023-03-18
 */
@Controller
@RequestMapping(URLUtil.CATEGORY)
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    @GetMapping(URLUtil.PAGE)
    public String index() {
        return URLUtil.CATEGORY_HTML;
    }

    // 上传分类图
    @PostMapping(URLUtil.IMAGE_PATH)
    @ResponseBody
    public ResVO avatar(MultipartFile file){
        System.out.println("收到图片");
        return categoryService.avatar(file);
    }

    // 查询所有分类
    @GetMapping(URLUtil.LIST)
    @ResponseBody
    public LayuiTableVO list(){
        return categoryService.doList();
    }

    // 查询子分类
    @GetMapping(URLUtil.CHILD)
    @ResponseBody
    public List<Category> getCategoryList(Integer id) {
//        categoryService.list(new LambdaQueryChainWrapper<Category>().eq(Category::getParentId));
        return categoryService.getCategoryList(id);
    }

    // 执行删除操作
    @PostMapping(URLUtil.DELETE)
    @ResponseBody
    public void del(Integer id){
        categoryService.doDel(id);
    }

    // 删除的前置操作，是先查出没有子菜单的菜单
    @GetMapping(URLUtil.UID)
    @ResponseBody
    public Category selectById(@PathVariable(C.ID) Integer id){
        return categoryService.getById(id);
    }

    // 新增或修改
    @PostMapping(URLUtil.SAVE)
    @ResponseBody
    public void save(Category category){
        categoryService.saveOrUpdate(category);
    }




}
